from contactApp.models import Ad
from django.shortcuts import render
from .forms import ResumeForm

# Create your views here.
def contact(request):
    return render(request,'contact.html')

def recruit(request):
    AdList = Ad.objects.all().order_by('-publishDate')
    if request.method == 'POST':
        # 参数data用来从request.POST中获取对应的数据，files用来接收对应的文件(此处指用户上传文件)
        resumeForm = ResumeForm(data=request.POST, files=request.FILES)
        if resumeForm.is_valid(): #is_valid()函数用来验证表单各字段格式是否符合要求；符合则通过接下来的resumeForm.save()进行数据保存 
            resumeForm.save()
            return render(request, 'success.html', {
                'active_menu': 'contactus',# 为什么active_menu是contactus,而'sub_menu'是'recruit'
                'sub_menu': 'recruit',
            })
    else:
        resumeForm = ResumeForm() # 处于非提交状态，则通过ResumeForm()建立非带参的模板表单变量，然后将该变量一起返回给前端
    return render(
        request, 'recruit.html', {
            'active_menu': 'contactus',
            'sub_menu': 'recruit',
            'AdList': AdList,
            'resumeForm': resumeForm,
        })